package feizhou.untils.tools;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MailUtil
{
    public static String SERVERHOST;
    
    public static Integer SERVERPORT;
    
    public static String FROM;
    
    public static String USERNAME;
    
    public static String PASSWORD;
    
    public static String PROTOCOL;
    
    public static String EMAILCONTENT;
    
    public static String SERVERURL;
    
    public static String[] RECEIVEUSERNAME;
    
    static {
        InputStream is = MailUtil.class.getClassLoader().getResourceAsStream("mail.properties");
        Properties property = new Properties();
        try {
            property.load(is);
            SERVERHOST = property.getProperty("serverHost");
            SERVERPORT = Integer.valueOf(property.getProperty("serverPort"));
            FROM = property.getProperty("from");
            USERNAME = property.getProperty("username");
            PASSWORD = property.getProperty("password");
            PROTOCOL = property.getProperty("protocol");
            SERVERURL = property.getProperty("serverUrl");
            RECEIVEUSERNAME = StringUtil.isEmpty(property.getProperty("receiveUsername"))?property.getProperty("receiveUsername").split(","):null;
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.print("获取properties异常");
            e.printStackTrace();
            
        }
    }
    
    
    /**给人发送邮件*/
	public static boolean sendEmail(String toEmail,String subject,String content){
		if(toEmail==null||"".equalsIgnoreCase(toEmail.trim()))return false;
		JavaMailSenderImpl javaMail = new JavaMailSenderImpl();
		/**email server*/
		javaMail.setHost(SERVERHOST);
		javaMail.setPort(SERVERPORT);
		javaMail.setProtocol(PROTOCOL);
		/**发送帐号配置*/
		javaMail.setUsername(USERNAME);
		javaMail.setPassword(PASSWORD);

		Properties prop = new Properties();
		prop.setProperty("mail.smtp.auth", "true");
		javaMail.setJavaMailProperties(prop);
		MimeMessage message = javaMail.createMimeMessage();
		MimeMessageHelper messageHelp;
		try {
			messageHelp = new MimeMessageHelper(message, true, "GBK");
			messageHelp.setFrom(FROM);
			messageHelp.setTo(toEmail);
			messageHelp.setSubject(subject);
			messageHelp.setText(content, true);
			javaMail.send(message);
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 发送邮件给多个人
	 * @Autor : xiongjinpeng  jpx_011@163.com
	 * @Date  : 2016年12月1日 下午3:13:38 
	 * @param toEmail
	 * @param subject
	 * @param content
	 * @return
	 */
	public static boolean sendEmailMany(String[] toEmail,String subject,String content){
		if(toEmail==null||toEmail.length==0)return false;
		JavaMailSenderImpl javaMail = new JavaMailSenderImpl();
		/**email server*/
		javaMail.setHost(SERVERHOST);
		javaMail.setPort(SERVERPORT);
		javaMail.setProtocol(PROTOCOL);
		/**发送帐号配置*/
		javaMail.setUsername(USERNAME);
		javaMail.setPassword(PASSWORD);

		Properties prop = new Properties();
		prop.setProperty("mail.smtp.auth", "true");
		javaMail.setJavaMailProperties(prop);
		MimeMessage message = javaMail.createMimeMessage();
		MimeMessageHelper messageHelp;
		try {
			messageHelp = new MimeMessageHelper(message, true, "GBK");
			messageHelp.setFrom(FROM);
			messageHelp.setTo(toEmail);
			messageHelp.setSubject(subject);
			messageHelp.setText(content, true);
			javaMail.send(message);
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	/**给人发送邮件*/
	public static boolean sendEmailForImportGpsies(String subject,String content){
		if(RECEIVEUSERNAME==null||RECEIVEUSERNAME.length==0)return false;
		JavaMailSenderImpl javaMail = new JavaMailSenderImpl();
		/**email server*/
		javaMail.setHost(SERVERHOST);
		javaMail.setPort(SERVERPORT);
		javaMail.setProtocol(PROTOCOL);
		/**发送帐号配置*/
		javaMail.setUsername(USERNAME);
		javaMail.setPassword(PASSWORD);

		Properties prop = new Properties();
		prop.setProperty("mail.smtp.auth", "true");
		javaMail.setJavaMailProperties(prop);
		MimeMessage message = javaMail.createMimeMessage();
		MimeMessageHelper messageHelp;
		try {
			messageHelp = new MimeMessageHelper(message, true, "GBK");
			messageHelp.setFrom(FROM);
			messageHelp.setTo(RECEIVEUSERNAME);
			messageHelp.setSubject(subject);
			messageHelp.setText(content, true);
			javaMail.send(message);
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
    public static void main(String[] args) {
        System.out.println(SERVERHOST);
        sendEmail("jinpeng.xiong@lolaage.com", "服务告警", "<span style='font-weight: bold'>你好</spn><span style='color:red'>你好</span>");
    }
}
